﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ResourceDll;
using System.IO;
using System.Diagnostics;

namespace BackupRestoreApplication.Develop
{
    public partial class frmASC : Form
    {
        BackgroundWorker m_oWorker;
        public frmASC()
        {
            InitializeComponent();
            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
        }


        #region BackgroundWorker

        void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                lbtxtStatus.Text = "Task Cancelled.";
            }
            else if (e.Error != null)
            {
                lbtxtStatus.Text = "Error while performing background operation.";
            }
            else
            {
                lbtxtStatus.Text = "Task Completed...";
            }
        }
        void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lbtxtStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }
        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(50);
                m_oWorker.ReportProgress(i);
                if (m_oWorker.CancellationPending)
                {
                    e.Cancel = true;
                    m_oWorker.ReportProgress(0);
                    return;
                }
            }
            m_oWorker.ReportProgress(100);
        }
        #endregion

        #region Button //////////
        private void btBackup_Click(object sender, EventArgs e)
        {
            this.btBackup.Enabled = false;
            this.btRestore.Enabled = false;
            this.btCancel.Enabled = false;
            _DeleteASCBK7();
            //_DeleteASCDownloader7();
            _DeleteASCLog7();
            _DeleteASCTemp7();
            _DeleteASCBK6();
            //_DeleteASCDownloader6();
            _DeleteASCLog6();
            _DeleteASCTemp6();
            _BKASC7();
            _BKASC6();
            _BKSD();
            m_oWorker.RunWorkerAsync();
            lbtxtFinish.Text = "Job : Backup !";
            this.btBackup.Enabled = true;
            this.btRestore.Enabled = true;
            this.btCancel.Enabled = true;
        }

        private void btRestore_Click(object sender, EventArgs e)
        {
            this.btBackup.Enabled = false;
            this.btRestore.Enabled = false;
            this.btCancel.Enabled = false;
            _REASC7();
            _REASC6();
            _RESD();
            m_oWorker.RunWorkerAsync();
            lbtxtFinish.Text = "Job : Restore !";
            this.btBackup.Enabled = true;
            this.btRestore.Enabled = true;
            this.btCancel.Enabled = true;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        private void frmASC_Load(object sender, EventArgs e)
        {
            this.pictureBox1.Image = ResourceDll.Img.asc;
        }

        #region ASC7

        private void _BKASC7()
        {

            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Advanced SystemCare 7\ASC.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Advanced SystemCare 7\ASC.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "ASC" || app.ProcessName == "ASCTray" || app.ProcessName == "Monitor")
                            app.Kill();
                    string source = _7zDll.pathroaming + @"\IObit.\Advanced SystemCare V7\*";
                    string target = Application.StartupPath + @"\DBBACKUP\ASC\ASC7.7z";
                    _7zDll.Compress(source, target);
                }

            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        private void _REASC7()
        {
            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Advanced SystemCare 7\ASC.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Advanced SystemCare 7\ASC.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "ASC" || app.ProcessName == "ASCTray" || app.ProcessName == "Monitor")
                            app.Kill();
                    string source = Application.StartupPath + @"\DBBACKUP\ASC\ASC7.7z";
                    string dest = _7zDll.pathroaming + @"\IObit\Advanced SystemCare V7\";
                    _7zDll.ExtractFile(source, dest);
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        #endregion

        #region ASC6

        private void _BKASC6()
        {

            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Advanced SystemCare 6\ASC.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Advanced SystemCare 6\ASC.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "ASC" || app.ProcessName == "ASCTray" || app.ProcessName == "Monitor")
                            app.Kill();
                    string source = _7zDll.pathroaming + @"\IObit.\Advanced SystemCare V6\*";
                    string target = Application.StartupPath + @"\DBBACKUP\ASC\ASC6.7z";
                    _7zDll.Compress(source, target);
                }

            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        private void _REASC6()
        {
            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Advanced SystemCare 6\ASC.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Advanced SystemCare 6\ASC.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "ASC" || app.ProcessName == "ASCTray" || app.ProcessName == "Monitor")
                            app.Kill();
                    string source = Application.StartupPath + @"\DBBACKUP\ASC\ASC6.7z";
                    string dest = _7zDll.pathroaming + @"\IObit\Advanced SystemCare V6\";
                    _7zDll.ExtractFile(source, dest);
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        #endregion


        #region Delete ASC 7
        private void _DeleteASCLog7()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V7\Log";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }

        private void _DeleteASCBK7()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V7\Backup";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }
        //private void _DeleteASCDownloader7()
        //{
        //    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V7\Downloader";
        //    System.IO.DirectoryInfo di = new DirectoryInfo(path);
        //    foreach (FileInfo file in di.GetFiles())
        //    {
        //        file.Delete();
        //    }
        //    foreach (DirectoryInfo dir in di.GetDirectories())
        //    {
        //        dir.Delete(true); //delete subdirectories and files
        //    }
        //}

        private void _DeleteASCTemp7()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V7\Temp";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }
        #endregion

        #region Delete ASC 6
        private void _DeleteASCLog6()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V6\Log";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }

        private void _DeleteASCBK6()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V6\Backup";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }

        //private void _DeleteASCDownloader6()
        //{
        //    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V6\Downloader";
        //    System.IO.DirectoryInfo di = new DirectoryInfo(path);
        //    foreach (FileInfo file in di.GetFiles())
        //    {
        //        file.Delete();
        //    }
        //    foreach (DirectoryInfo dir in di.GetDirectories())
        //    {
        //        dir.Delete(true); //delete subdirectories and files
        //    }
        //}

        private void _DeleteASCTemp6()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\IObit\Advanced SystemCare V6\Temp";
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); //delete subdirectories and files
            }
        }
        #endregion

        #region Smart Defrag

        private void _BKSD()
        {

            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Smart Defrag 2\SmartDefrag.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Smart Defrag 2\SmartDefrag.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "SmartDefrag")
                            app.Kill();
                    string source = _7zDll.pathroaming + @"\IObit.\Smart Defrag 2\*";
                    string target = Application.StartupPath + @"\DBBACKUP\ASC\SD.7z";
                    _7zDll.Compress(source, target);
                }

            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        private void _RESD()
        {
            try
            {
                string checkf1 = frmMain.path1 + @"\IObit\Smart Defrag 2\SmartDefrag.exe";
                string checkf2 = frmMain.path2 + @"\IObit\Smart Defrag 2\SmartDefrag.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "SmartDefrag")
                            app.Kill();
                    string source = Application.StartupPath + @"\DBBACKUP\ASC\SD.7z";
                    string dest = _7zDll.pathroaming + @"\IObit\Smart Defrag 2\";
                    _7zDll.ExtractFile(source, dest);
                }
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }
        #endregion
    }
}
